import java.util.*;

public class Solution451 {
    public String frequencySort(String s) {
        Map<Character,Integer> map=new HashMap<>();
        for (char c : s.toCharArray()) {
            map.put(c,map.getOrDefault(c,0)+1);
        }
        List<Map.Entry<Character,Integer>> l=new ArrayList<>(map.entrySet());
        Collections.sort(l,(a,b)->b.getValue()-a.getValue());
        StringBuilder stringBuilder=new StringBuilder();
        for (Map.Entry<Character, Integer> integerEntry : l) {
            for (int i=0;i<integerEntry.getValue();i++){
                stringBuilder.append(integerEntry.getKey());
            }
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution451().frequencySort("tree"));
    }
}
